
package com.breaktian.assemble.adapter;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
 * Created by breaktian on 2016/8/23.
 * 权限适配器 为了适配android6.0以上的权限问题
 * 建议在BaseActivity中使用此类
 * 添加如下代码:
 *  mPermissionAdapter = new PermissionAdapter(this);
 *   @Override
 *   public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
 *      mPermissionAdapter.onRequestPermissionsResult(requestCode,permissions,grantResults);
 *   }
 *
 */
public class PermissionAdapter {

    private final int REQ_PERMISSION_SMS = 0x1;
    private final int REQ_PERMISSION_PHONE = 0x2;
    private final int REQ_PERMISSION_LOCATION = 0x3;
    private final int REQ_PERMISSION_CONTACTS = 0x4;
    private final int REQ_PERMISSION_CAMERA = 0x5;
    private final int REQ_PERMISSION_MICROPHONE = 0x6;
    private final int REQ_PERMISSION_STORAGE = 0x7;
	private final int REQ_PERMISSION_CALENDAR = 0x8;

    private Activity context;
    private PermissionVerifyCallBack permissionCallback;

    public PermissionAdapter(Activity context) {
        this.context = context;
    }

    public boolean verifyPermissions(int[] grantResults) {
        // At least one result must be checked.
        if (grantResults.length < 1) {
            return false;
        }
        // Verify that each required permission has been granted, otherwise return false.
        for (int result : grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    public interface PermissionVerifyCallBack {
        void onPermissionGet();

        void onPermissionLost();
    }

	public void requestCalendarPermission(PermissionVerifyCallBack callback) {
		this.permissionCallback = callback;
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
			ActivityCompat.requestPermissions(context, new String[] {
					Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR },
					REQ_PERMISSION_CALENDAR);
		} else {
			this.permissionCallback.onPermissionGet();
		}
	}

	public void requestCameraAndMicrophonePermission(PermissionVerifyCallBack callback) {
		this.permissionCallback = callback;
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
			ActivityCompat.requestPermissions(context,
					new String[] { Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA },
					REQ_PERMISSION_MICROPHONE);
		} else {
			this.permissionCallback.onPermissionGet();
		}
	}

    public void requestMicrophonePermission(PermissionVerifyCallBack callback) {
        this.permissionCallback = callback;
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
			ActivityCompat.requestPermissions(context,
					new String[] { Manifest.permission.RECORD_AUDIO }, REQ_PERMISSION_MICROPHONE);
        } else {
            this.permissionCallback.onPermissionGet();
        }
    }

    public void requestCameraPermission(PermissionVerifyCallBack callback) {
        this.permissionCallback = callback;
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(context, new String[] {
                    Manifest.permission.CAMERA
            }, REQ_PERMISSION_CAMERA);
        } else {
            this.permissionCallback.onPermissionGet();
        }
    }

    public void requestContactsPermission(PermissionVerifyCallBack callback) {
        this.permissionCallback = callback;
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED
		// ||ContextCompat.checkSelfPermission(context,Manifest.permission.READ_CONTACTS) !=
		// PackageManager.PERMISSION_GRANTED
		// ||ContextCompat.checkSelfPermission(context,Manifest.permission.WRITE_CONTACTS) !=
		// PackageManager.PERMISSION_GRANTED
		) {
			ActivityCompat.requestPermissions(context,
					new String[] { Manifest.permission.GET_ACCOUNTS }, REQ_PERMISSION_CONTACTS);
        } else {
            this.permissionCallback.onPermissionGet();
        }
    }

    public void requestLocationPermission(PermissionVerifyCallBack callback) {
        this.permissionCallback = callback;
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
			ActivityCompat
					.requestPermissions(context,
							new String[] { Manifest.permission.ACCESS_FINE_LOCATION,
									Manifest.permission.ACCESS_COARSE_LOCATION },
							REQ_PERMISSION_LOCATION);
        } else {
            this.permissionCallback.onPermissionGet();
        }
    }

    public void requestPhonePermission(PermissionVerifyCallBack callback) {
        this.permissionCallback = callback;
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
		// ||ContextCompat.checkSelfPermission(context,Manifest.permission.CALL_PHONE)!=
		// PackageManager.PERMISSION_GRANTED
		// ||ContextCompat.checkSelfPermission(context,Manifest.permission.READ_CALL_LOG)!=
		// PackageManager.PERMISSION_GRANTED
		// ||ContextCompat.checkSelfPermission(context,Manifest.permission.WRITE_CALL_LOG)!=
		// PackageManager.PERMISSION_GRANTED
		// ||ContextCompat.checkSelfPermission(context,Manifest.permission.ADD_VOICEMAIL)!=
		// PackageManager.PERMISSION_GRANTED
		// ||ContextCompat.checkSelfPermission(context,Manifest.permission.USE_SIP)!=
		// PackageManager.PERMISSION_GRANTED
		// ||ContextCompat.checkSelfPermission(context,Manifest.permission.PROCESS_OUTGOING_CALLS)!=
		// PackageManager.PERMISSION_GRANTED
		) {
			ActivityCompat.requestPermissions(context,new String[] { Manifest.permission.READ_PHONE_STATE }, REQ_PERMISSION_PHONE);
        } else {
            this.permissionCallback.onPermissionGet();
        }
    }

    public void requestStoragePermission(PermissionVerifyCallBack callback) {
        this.permissionCallback = callback;
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
			ActivityCompat
					.requestPermissions(context,
							new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE,
									Manifest.permission.READ_EXTERNAL_STORAGE },
							REQ_PERMISSION_STORAGE);
        } else {
            this.permissionCallback.onPermissionGet();
        }
    }

    public void requestSmsPermission(PermissionVerifyCallBack callback) {
        this.permissionCallback = callback;
		if (ContextCompat.checkSelfPermission(context,
				Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED
				|| ContextCompat.checkSelfPermission(context,
						Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED
		// || ContextCompat.checkSelfPermission(context,Manifest.permission.RECEIVE_WAP_PUSH) !=
		// PackageManager.PERMISSION_GRANTED
		// || ContextCompat.checkSelfPermission(context,Manifest.permission.RECEIVE_MMS) !=
		// PackageManager.PERMISSION_GRANTED
		) {
            ActivityCompat.requestPermissions(context, new String[] {
                    Manifest.permission.SEND_SMS,
					Manifest.permission.READ_SMS, Manifest.permission.RECEIVE_SMS
            }, REQ_PERMISSION_SMS);
        } else {
            this.permissionCallback.onPermissionGet();
        }
    }

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
            @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQ_PERMISSION_SMS:
            case REQ_PERMISSION_STORAGE:
            case REQ_PERMISSION_PHONE:
            case REQ_PERMISSION_LOCATION:
            case REQ_PERMISSION_CONTACTS:
            case REQ_PERMISSION_CAMERA:
            case REQ_PERMISSION_MICROPHONE:
		case REQ_PERMISSION_CALENDAR:
                if (permissionCallback != null) {
                    if (verifyPermissions(grantResults)) {
                        permissionCallback.onPermissionGet();
                    } else {
                        permissionCallback.onPermissionLost();
                    }
                    permissionCallback = null;
                }
                break;
            default:
                break;
        }
    }
}
